package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.TbCustomerEventsMapper;
import com.it.tydic.estate.module.mapper.inner.TbCustomerProfileMapper;
import com.it.tydic.estate.module.model.Vo.ReturnObjectVo;
import com.it.tydic.estate.module.model.inner.TbCustomerEvents;
import com.it.tydic.estate.module.service.CustomerEventsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liuhui
 * @createDate: 2020/3/5
 * @version: 1.0
 */
@Transactional
@Service
public class CustomerEventsServiceImpl implements CustomerEventsService {
    @Autowired
    private TbCustomerEventsMapper customerEventsMapper;  /*事件*/
    @Autowired
    private TbCustomerProfileMapper tbCustomerProfileMapper;  /*档案*/

    /**
     * 查询
     * @param map
     * @return
     */
    @Override
    public Result selectListEvents(Map<String, Object> map) {
        /*分页*/
//        if(map.containsKey("pageNum") && map.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
//        }
        CommonUtils.pageProcess(map);//验证处理分页
        Page<TbCustomerEvents> tbCustomerEvents = customerEventsMapper.selectListEventsByLike(map);
        List<TbCustomerEvents> result = tbCustomerEvents.getResult();
        if(result.size()>0 && result != null){
            /*码表转换*/
            for (TbCustomerEvents customerEvents : result) {
                customerEvents.setImportanceLevel(DictionaryCache.getDicVal(customerEvents.getImportanceLevel()));
            }
        }
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("result",result);
        resultMap.put("totle",tbCustomerEvents.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 获取详情
     * @param uid
     * @return
     */
    @Override
    public Result getById(String uid) {
        /*参数验证*/
        if(StringUtils.isNotEmpty(uid))
            return Result.success(customerEventsMapper.selectByPrimaryKey(uid));
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }

    /**
     * 添加修改
     * @param tbCustomerEvents
     * @return
     */
    @Override
    public Result addEvents(TbCustomerEvents tbCustomerEvents) {
        int num = 0 ;
        /*生成入单时间*/
        tbCustomerEvents.setRecordingDate(new Date());
        if(StringUtils.isNotEmpty(tbCustomerEvents.getUid())){  /*修改*/
            /*创建修改人*/
            tbCustomerEvents.setUuserId(CurrentUtil.getCurrentUserId());
            tbCustomerEvents.setXgsj(new Date());
            num =customerEventsMapper.updateByPrimaryKeySelective(tbCustomerEvents);
            return num >0 ? Result.success(tbCustomerEvents.getUid()) : Result.error(CodeMsg.UPDATE_ERROR);
        }else{
            /*创建修改人*/
            tbCustomerEvents.setCuserId(CurrentUtil.getCurrentUserId());
            tbCustomerEvents.setCjsj(new Date());
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
            if(tbCustomerEvents.getZfbj() == null || tbCustomerEvents.getZfbj().equals(" ")){ tbCustomerEvents.setZfbj(ZfbjFinalUtil.zfbj_zy);}
            tbCustomerEvents.setUid(String.valueOf(id));
            num = customerEventsMapper.insertSelective(tbCustomerEvents);
        }
        return num >0 ? Result.success(tbCustomerEvents.getUid()) : Result.error(CodeMsg.INSERT_ERROR);
    }

    /**
     * 删除
     * @param list
     * @return
     */
    @Override
    public Result delEvents(List<String> list) {
        /*参数验证*/
        if(list.size()< 1 || list == null)
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        return customerEventsMapper.deleteByList(list) >0 ?Result.success() : Result.error(CodeMsg.DELETE_ERROR);
    }

    /**
     * 获取类型
     * @param codeId
     * @return
     */
    @Override
    public Result getType(String codeId) {
        /*参数验证*/
        if(StringUtils.isNotEmpty(codeId))
            return Result.success(customerEventsMapper.getType(codeId));
         return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }

    /**
     * 级联查询
     * @param map
     * @return
     */
    @Override
    public Result selectCascade(Map<String, String> map) {
        if(map.containsKey("type")){
            if(map.get("type").equals("glc")){  /*管理处*/
                if(map.containsKey("userCode")){
                    return Result.success(tbCustomerProfileMapper.getGLC(map.get("userCode")));
                }else{return Result.error(CodeMsg.EMPTY_PARAM_ERROR);}
            }else if (map.get("type").equals("ly")){ /*楼宇*/
                if(map.containsKey("xqid")){
                   return  Result.success(customerEventsMapper.selectLy(map.get("xqid")));
                }
            }else if(map.get("type").equals("fj")){ /*房间*/
                if(map.containsKey("lyid")){
                    return  Result.success(customerEventsMapper.selectRoom(map.get("lyid")));
                }
            }
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }
}
